package ar.com.ford.it.fullEconomics.logic;

import java.sql.Timestamp;
import java.util.Collection;
import ar.com.ford.it.fullEconomics.FullEconomicsPrs;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.persistence.dao.Dao;
import ar.com.ford.it.fullEconomics.persistence.dao.WorkerFileDao;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesx01Intfsource;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesx02Fileconten;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;

public class WorkerFileLogic extends Dao {
    private static final String CLASS_NAME = WorkerFileLogic.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);
    private static final String USER_BACH = "batchee";
    private static final String NAME_PROCESS = "FTPGET";
   
    /**
     * 
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesx01Intfsource> findIntfSourceByFlag(String[] flag)
            throws FullEconomicsException {

        String METHOD_NAME = "findIntfSourceByFlag";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesx01Intfsource> res = null;
        WorkerFileDao dao = new WorkerFileDao();
        try {
            res = dao.findIntfSourceByFlag(flag);            
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Find by Flag IntfSource Value Exception", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.EconomicValue.FINDALL);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }
    
    
    /**
	 * Guarda un nuevo FileConten
	 * 
	 * @param teesx01Intfsource
	 * @param user
	 * @throws FullEconomicsException
	 */
	public void createFileConten(Teesx01Intfsource teesx01Intfsource, byte[] file, Integer size)
			throws FullEconomicsException {

		String METHOD_NAME = "saveFileConten";
		log.entering(CLASS_NAME, METHOD_NAME);

		WorkerFileDao dao = new WorkerFileDao();
		
		Teesx02Fileconten newFileconten = new Teesx02Fileconten();		
		
		//String[] fields = new String[1];
        //Object[] values = new Object[1];

		try {
			PersistenceUtil.newTransaction();
			
			Timestamp fechaActual = new Timestamp(System.currentTimeMillis());			
            
			Integer maxSequence = dao.getMax(new Teesx02Fileconten(), null,
                    null, "eesx02FconSequenceK");
			
			newFileconten.setEesx01Intfsource(teesx01Intfsource);
			newFileconten.setEesx02FconSequenceK(maxSequence + 1);	
			newFileconten.setEesx02FconGenerationDateM(fechaActual);
			newFileconten.setEesx02FconContentSizeQ(size);
			newFileconten.setEesx02FconFileBlobO(file);
			
			newFileconten.setEesx02CreateS(fechaActual);
			newFileconten.setEesx02CreateUserC(USER_BACH);
			newFileconten.setEesx02LastUpdtS(fechaActual);
			newFileconten.setEesx02LastUpdtUserC(USER_BACH);
			newFileconten.setEesx02CreateProcessC(NAME_PROCESS);
			newFileconten.setEesx02LastUpdtProcessC(NAME_PROCESS);		


			dao.createFileConten(newFileconten);
			PersistenceUtil.commitTransaction();

		} catch (PersistenceException pe) {

			try {
				PersistenceUtil.rollbackTransaction();
			} catch (PersistenceException rollbackEx) {
				rollbackEx.printStackTrace();
			}

			log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
					"Save FileConten Exception", pe);
			throw new FullEconomicsException(pe,
					FullEconomicsPrs.MessageErrors.ProductionVolumesMix.CREATE);
		}

		log.exiting(CLASS_NAME, METHOD_NAME);
	}
	
}
